package com.vsked.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class ServletProcess {
	public static String forServletAddParmReg="";
	public static String forServletEditParReg="";
	public static String getImportInfo(){
		return Symbol.enterSymbol+"import javax.servlet.*;"+Symbol.enterSymbol+"import javax.servlet.http.*;"+""+Symbol.enterSymbol+"import java.io.*;"+Symbol.enterSymbol+"import java.util.*;"+Symbol.enterSymbol+"import util.*;"+Symbol.enterSymbol;
	}
	public static String getDoGetMethod(){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"this.processRequest(request, response);"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public static String getDoPostMethod(){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"this.processRequest(request, response);"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public static String getServlet(String inServletName,String inServletContent){
		return Symbol.enterSymbol+"public class "+Symbol.getStringFirstUpperCase(inServletName)+"Servlet extends HttpServlet{"+inServletContent+ServletProcess.getDoGetMethod()+ServletProcess.getDoPostMethod()+Symbol.enterSymbol+"}";
	}
	
	public static String getProcessRequest(String inRequestContent){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"String method = request.getParameter(\"method\")==null?\"\":request.getParameter(\"method\");"+inRequestContent+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";
	}
	public static String getRequestParameter(String inParameter){
		return "request.getParameter(\""+inParameter+"\");";
	}
	public static String getRequestParameterValue(String inFieldName,String inDataType){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+inDataType+Symbol.getSpaceString(1)+inFieldName+"=request.getParameter(\""+inFieldName+"\");";
	}
	
	public static String getWebXMLString(String inServletName,String inServletClass){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"<servlet>"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"<servlet-name>"+inServletName+"</servlet-name>"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"<servlet-class>"+inServletClass+"</servlet-class>"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"</servlet>"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"<servlet-mapping>"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"<servlet-name>"+inServletName+"</servlet-name>"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"<url-pattern>/"+inServletName+"</url-pattern>"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"</servlet-mapping>";
	}
	public static String getAddString(String inAddStringBody,String inTableName,String inModulePath){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+"if(\"add\".equals(method)){"+inAddStringBody+Symbol.enterSymbol+Symbol.getSpaceString(12)+"goList(request, response);"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"}";
	}
	public static String getDeleteString(String inDeleteStringBody,String inTableName,String inModulePath){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+"if(\"del\".equals(method)){"+inDeleteStringBody+Symbol.enterSymbol+Symbol.getSpaceString(12)+"goList(request, response);"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"}";
	}
	public static String getEditString(String inEditStringBody,String inTableName,String inModulePath){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+"if(\"edit\".equals(method)){"+inEditStringBody+Symbol.enterSymbol+Symbol.getSpaceString(12)+"goList(request, response);"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"}";
	}
	public static String getListString(String inListStringBody){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+"if(\"list\".equals(method)){"+inListStringBody+Symbol.enterSymbol+Symbol.getSpaceString(8)+"}";	
	}
	public static String getSetSessionAttribute(String inSessionAttributeName,Object inSessionAttributeValue){
		return Symbol.enterSymbol+Symbol.getSpaceString(12)+"request.getSession().setAttribute("+inSessionAttributeName+", "+inSessionAttributeValue+");";
	}
	public static String getShowAddString(String inAddStringBody,String inTableName,String inModulePath){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+"if(\"showadd\".equals(method)){"+Symbol.enterSymbol+Symbol.getSpaceString(12)+inAddStringBody+Symbol.enterSymbol+Symbol.getSpaceString(12)+"request.getSession(true).removeAttribute(\""+Symbol.getStringFirstLowerCase(inTableName)+"\");"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"response.sendRedirect(\""+inModulePath+"/"+Symbol.getStringFirstUpperCase(inTableName)+"Operate.jsp\");"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"}";
	}
	public static String getGoListString(String inTableName,String inModulePath){
		return Symbol.enterSymbol+Symbol.getSpaceString(4)+"public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"request.getSession(true).setAttribute(\""+Symbol.getStringFirstLowerCase(inTableName)+"List\",new "+Symbol.getStringFirstUpperCase(inTableName)+"DAO().get"+Symbol.getStringFirstUpperCase(inTableName)+"All());"+Symbol.enterSymbol+Symbol.getSpaceString(8)+"response.sendRedirect(\""+inModulePath+"/"+Symbol.getStringFirstUpperCase(inTableName)+"List.jsp\");"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"}";		
	}
	public static String getDAOProcessForAddOrEdit(String inNewDaoProcess,String inSuccessInfo,String inFailInfo){
		return Symbol.enterSymbol+Symbol.getSpaceString(12)+"if("+inNewDaoProcess+"){"+inSuccessInfo+Symbol.enterSymbol+Symbol.getSpaceString(12)+"}else{"+Symbol.getSpaceString(12)+inFailInfo+Symbol.enterSymbol+Symbol.getSpaceString(12)+"}";
	}
	public static String getDAOProcessForDel(String inDaoDel,String inSuccessInfo,String inFailInfo){
		return Symbol.enterSymbol+Symbol.getSpaceString(12)+"if("+inDaoDel+"){"+Symbol.getSpaceString(12)+""+inSuccessInfo+Symbol.enterSymbol+Symbol.getSpaceString(12)+"}else{"+Symbol.getSpaceString(12)+inFailInfo+Symbol.enterSymbol+Symbol.getSpaceString(12)+"}";
	}
	public static String getDAOProcessForList(){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+"if (\"list\".equals(method)) goList(request, response); ";	
	}
	public static String getDAOProcessForListById(String inTableName,String inPrimarykey,String inModulePath){
		return Symbol.enterSymbol+Symbol.getSpaceString(8)+"if (\"listById\".equals(method)) {"+Symbol.enterSymbol+Symbol.getSpaceString(12)+" request.getSession(true).setAttribute(\""+Symbol.getStringFirstLowerCase(inTableName)+"\",new "+Symbol.getStringFirstUpperCase(inTableName)+"DAO().get"+Symbol.getStringFirstUpperCase(inTableName)+"By"+Symbol.getStringFirstUpperCase(inPrimarykey)+"(Integer.parseInt(request.getParameter(\""+Symbol.getStringFirstLowerCase(inPrimarykey)+"\"))));"+Symbol.enterSymbol+Symbol.getSpaceString(12)+"response.sendRedirect(\""+inModulePath+"/"+Symbol.getStringFirstUpperCase(inTableName)+"Operate.jsp\");"+Symbol.enterSymbol+Symbol.getSpaceString(4)+"	}";
	}
	public static String getResponseSendRedirect(String inPagePath){
		return "response.sendRedirect(\""+inPagePath+"\");";
	}
	public static String getResponseSendRedirect(String inPagePath,String inParameter){
		return "response.sendRedirect(\""+inPagePath+inParameter+"\");";
	}
	public static void main(String[] args) {
	}
	
	
}
